# src/dependencies/auth.py
from fastapi import Header, HTTPException, Request
from src.config import settings


def verify_token(authorization: str = Header(...)):
    if not authorization.startswith("Bearer "):
        raise HTTPException(status_code=401, detail="无效的Token格式")

    token = authorization.split(" ")[1]
    if token != settings.API_TOKEN:
        raise HTTPException(status_code=401, detail="无效的Token")


def verify_ip(request: Request):
    client_ip = request.client.host
    if client_ip not in settings.IP_WHITELIST:
        raise HTTPException(status_code=403, detail=f"IP {client_ip}不在白名单中")
# 示例
# @app.post("/ocr/idcard/{side}", dependencies=[Depends(verify_token), Depends(verify_ip)])
# async def recognize_idcard(...):